package com.example.studentapp.login;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.studentapp.R;
import com.example.studentapp.db.SQLiteDB;

import java.text.SimpleDateFormat;
import java.util.Date;

public class RegisterActivity extends AppCompatActivity {
    ImageView register_returnIV;
    EditText register_phoneET,register_nicknameET,register_pwdET,register_mk_pwdET,register_emailET;
    Button registerBtn;
    SQLiteDB sqLiteDB;
    SQLiteDatabase sqLiteDatabase;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        register_returnIV = findViewById(R.id.register_returnIV);
        register_phoneET = findViewById(R.id.register_phoneET);
        register_nicknameET = findViewById(R.id.register_nicknameET);
        register_pwdET = findViewById(R.id.register_pwdET);
        register_emailET = findViewById(R.id.register_emailET);
        register_mk_pwdET = findViewById(R.id.register_mk_pwdET);
        registerBtn = findViewById(R.id.registerBtn);
        register_returnIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取输入账号信息
                String phone = register_phoneET.getText().toString();
                String nickName = register_nicknameET.getText().toString();
                String pwd = register_pwdET.getText().toString();
                String mkpwd = register_mk_pwdET.getText().toString();
                String email = register_emailET.getText().toString();
                // 保存到数据库
                // 校验数据是否正确
                if(phone.length() >0 && nickName.length()>0 && pwd.length()>0&& mkpwd.length()>0&& email.length()>0){
                    if(pwd.equals(mkpwd)){
                        if(email.contains("@") && email.contains(".")){
                            // 入库
                            try {
                                sqLiteDB = new SQLiteDB(getApplicationContext());
                                sqLiteDatabase = sqLiteDB.getWritableDatabase();
                                // 设置时间格式
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                String reg_date = simpleDateFormat.format(new Date());
                                // 插入数据
                                String insertSql="insert into user_info(phone,pwd,nickname,email,reg_date)" +
                                        " values('"+phone+"','"+pwd+"','"+nickName+"','"+email+"','"+reg_date+"')";
                                sqLiteDatabase.execSQL(insertSql);
                                finish();

                            }catch (Exception e){
                                Toast.makeText(RegisterActivity.this, getString(R.string.db_toast_add_error), Toast.LENGTH_SHORT).show();
                            }
                        }else{
                            Toast.makeText(RegisterActivity.this, getString(R.string.login_toast_format), Toast.LENGTH_SHORT).show();
                        }

                    }else{
                        Toast.makeText(RegisterActivity.this, getString(R.string.login_toast_same), Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(RegisterActivity.this, getString(R.string.login_toast_null), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}